package com.check.operation.fragment;

import android.content.BroadcastReceiver;
import android.content.DialogInterface;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.check.operation.CheckApplication;
import com.check.operation.R;
import com.check.operation.base.BaseFragment;
import com.check.operation.model.Inpatient;
import com.check.operation.model.MessagRe;
import com.check.operation.model.Message;
import com.check.operation.model.Operation;
import com.check.operation.model.RequestUse;
import com.check.operation.service.SshdAdapter;
import com.szj.util.HttpUtil;
import com.szj.util.JSONUtil;
import com.szj.util.SysUtil;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.App;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 手术核对（术中）
 */
@EFragment(R.layout.fragment_sz)
public class SzFragment extends BaseFragment implements AdapterView.OnItemClickListener {

    BroadcastReceiver receiver;
    public String action;
    public static final String TAG="INSZ";

    @App
    CheckApplication sshcApp;
    @ViewById
    TextView smh;
    @ViewById
    ListView sqlb;
    @ViewById
    TextView  zyh;
    @ViewById
    TextView xm;
    @ViewById
    TextView xb;
    @ViewById
    TextView ks;

    String tmsmh;
    final String sszt="j";
    final String ssztmc="术中";

    SshdAdapter sshdAdapter;

    List<Operation>  operations;

    @AfterViews
    public void initview(){
        sshdAdapter=new SshdAdapter(getLayoutInflater(),sshcApp.getSsrooms(),sshcApp.getSsbwall(),sshcApp.getUserRe().getUser());
    }

    @Background
    public void refdownsj(final String zyh){
        initdata(zyh);
        String action="patient/patientinfo";
        Map<String,Object> params=sshcApp.getParams();

        params.put("user",sshcApp.getUserRe().getUser());
        params.put("request", new Object(){
            public String barCode=zyh;
        });
        String rs=HttpUtil.postJSON(action, JSONObject.toJSONString(params));
        if(SysUtil.isNotNUll(rs)){
            if(rs.indexOf("message")<=0){
                Message message=(Message) JSONUtil.fromJson(rs, Message.class);
                alert(message);
            }else{
                MessagRe<Inpatient> inp=JSONObject.parseObject(rs, new TypeReference<MessagRe<Inpatient>>(){});
                operations=inp.value.operations;
                uiInpat(inp);
                uiOpre(operations);
            }

        }
        sqlb.setOnItemClickListener(this);
    }

    @UiThread
    public void initdata(String tmh){
        sshdAdapter.setCws(new ArrayList<Operation>());
        sshdAdapter.notifyDataSetInvalidated();

        smh.setText(tmh);
        zyh.setText(null);
        xm.setText(null);
        xb.setText(null);
        ks.setText(null);
    }

    @UiThread
    public void uiInpat(MessagRe<Inpatient> inp){
        Inpatient inpatient=inp.value;
        zyh.setText(inpatient.inpatient_no);
        xm.setText(inpatient.name);
        xb.setText(inpatient.sexname);
        ks.setText(inpatient.dept_name);
    }

    @UiThread
    public void uiOpre(List<Operation> operations){
        for(Operation op:operations){
            op.op_position_name=sshcApp.getDics().get("1021").get(op.op_position);
            op.process_status_name=sshcApp.getDics().get("1020").get(op.process_status);
        }
        sshdAdapter.setCws(operations);
        sqlb.setAdapter(sshdAdapter);
        sshdAdapter.notifyDataSetChanged();
    }


    @UiThread
    public void alert(Message message){
        Toast.makeText(getContext(), message.text, Toast.LENGTH_SHORT).show();
    }

    @UiThread
    public void alert(String message){
        Toast.makeText(getContext(), message, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, final View arg1, int arg2, long arg3) {
        final Operation operation=operations.get(arg2);
        new AlertDialog.Builder(getActivity()).setTitle(ssztmc+"？").setPositiveButton("是",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String action="process/process";
                        RequestUse requestUse=new RequestUse();
                        requestUse.user=sshcApp.getUserRe().getUser();
                        Map<String,Object> params=new HashMap<String, Object>();
                        params.put("surgicNo", operation.surgic_no);
                        params.put("processType", sszt);
                        params.put("position", operation.op_position);
                        params.put("room", operation.op_room_id);
                        requestUse.request=params;
                        String rs=HttpUtil.postJSON(action, JSONObject.toJSONString(requestUse));
                        TextView t1=(TextView) arg1.findViewById(R.id.sszt);
                        t1.setText(ssztmc);
                    }
                }).setNegativeButton("否",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {}
                }).show();
    }

    @Override
    public void onGetBarValue(String arg0) {
        refdownsj(arg0);
    }

}

